package com.car.serve.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.car.commons.core.model.Result;
import com.car.serve.Param.PageserviceReviewParam;
import com.car.serve.Param.addCommentsParam;
import com.car.serve.Param.clerkReplyParam;
import com.car.serve.Param.userReplyParam;
import com.car.serve.client.UserClient;
import com.car.serve.client.dto.findByUserIdDto;
import com.car.serve.entity.ServeInfo;
import com.car.serve.entity.ServeShouhou;
import com.car.serve.entity.ServiceReview;
import com.car.serve.exception.ServeException;
import com.car.serve.form.PageserviceReviewForm;
import com.car.serve.form.getUserReplyForm;
import com.car.serve.mapper.ServiceReviewMapper;
import com.car.serve.service.ServeInfoService;
import com.car.serve.service.ServiceReviewService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 董旭
 * @since 2024-08-28
 */
@Service
public class ServiceReviewServiceImpl extends ServiceImpl<ServiceReviewMapper, ServiceReview> implements ServiceReviewService {

    @Resource
    private ServiceReviewService serviceReviewService;
    @Resource
    private ServiceReviewMapper serviceReviewMapper;
    @Resource
    private ServeInfoService serveInfoService;
    @Resource
    private UserClient userClient;
    @Override
    public void addComments(addCommentsParam param) throws Exception {
        //根据ID查询用户姓名判断是否被禁言
        Result<findByUserIdDto> userById = userClient.findUserById(param.getUserId());
        if (userById.getCode()!=200)throw new ServeException(3000,"不存在用户");
        if (userById.getData().getUserState()!=0)throw new ServeException(3000,"账号当前不可用");
        if (userById.getData().getSilenceState()!=0)throw new ServeException(3000,"当前账号处于禁言状态无法评论");
        //新增评论
        ServiceReview serviceReview = BeanUtil.toBean(param, ServiceReview.class);
        //获取当前系统时间
        String createTime = DateUtil.format(LocalDateTime.now(), "yyyy-MM-dd HH:mm:ss");
        //获取用户电话
        String userPhone = userById.getData().getUserPhone();
        String userName = userById.getData().getUserName();
        //根据服务ID查询服务名
        ServeInfo byId = serveInfoService.getById(param.getServeId());
        String serveName = byId.getServeName();
        serviceReview.setCreatedTime(createTime);
        serviceReview.setComment(param.getComment());
        serviceReview.setUserId(param.getUserId());
        serviceReview.setUserPhone(userPhone);
        serviceReview.setUserName(userName);
        serviceReview.setServeId(param.getServeId());
        serviceReview.setServeName(serveName);
        //设置为0  0为首次评论
        serviceReview.setParentId(0);
        serviceReview.setStoresId(param.getStoresId());
        serviceReview.setRating(param.getRating());
        serviceReviewService.save(serviceReview);
    }

    @Override
    public void userReply(userReplyParam param) throws Exception {
        //根据ID查询用户姓名判断是否被禁言
        Result<findByUserIdDto> userById = userClient.findUserById(param.getUserId());
        //查询首评用户
        Result<findByUserIdDto> userById1 = userClient.findUserById(param.getUserIdComment());
        String userName1 = userById1.getData().getUserName();
        if (userById.getCode()!=200)throw new ServeException(3000,"不存在用户");
        if (userById.getData().getUserState()!=0)throw new ServeException(3000,"账号当前不可用");
        if (userById.getData().getSilenceState()!=0)throw new ServeException(3000,"当前账号处于禁言状态无法回复");
        //根据评论人ID查询服务I门店ID
        ServiceReview byId = serviceReviewService.getById(param.getUserIdComment());
        if (byId==null)throw new ServeException(3000,"没有评论");
        Integer serveId = byId.getServeId();
        String serveName = byId.getServeName();
        Integer storesId = byId.getStoresId();
        //新增回复
        ServiceReview serviceReview = BeanUtil.toBean(param, ServiceReview.class);
        //获取当前系统时间
        String createTime = DateUtil.format(LocalDateTime.now(), "yyyy-MM-dd HH:mm:ss");
        //获取用户电话
        String userPhone = userById.getData().getUserPhone();
        String userName = userById.getData().getUserName();
        serviceReview.setCreatedTime(createTime);
        serviceReview.setComment(param.getComment());
        serviceReview.setUserId(param.getUserId());
        serviceReview.setUserPhone(userPhone);
        serviceReview.setUserName(userName);
        serviceReview.setServeId(serveId);
        serviceReview.setServeName(serveName);

        //回复状态设置为评论人的Id
        serviceReview.setParentId(param.getUserIdComment());
        serviceReview.setParentName(userName1);
        serviceReview.setStoresId(storesId);
        serviceReviewService.save(serviceReview);
    }

    @Override
    public void clerkReply(clerkReplyParam param) throws Exception {
        if (param.getComment()==null)throw new ServeException(3000,"回复内容不能为空");
        //判断当前登录的电源状态是否正常
        Result clerkById = userClient.findClerkById();
        if (clerkById.getCode()!=200)throw new ServeException(clerkById.getCode(),clerkById.getMsg());
        //根据要对谁进行回复去查询信息
        ServiceReview byId = serviceReviewService.getById(param.getUserId());
        String userPhone = byId.getUserPhone();
        String userName = byId.getUserName();
        Integer storesId = byId.getStoresId();
        Integer serveId = byId.getServeId();
        //新增回复
        ServiceReview serviceReview = BeanUtil.toBean(param, ServiceReview.class);
        //获取系统时间
        String time = DateUtil.format(LocalDateTime.now(), "yyyy-MM-dd HH:mm:ss");
        serviceReview.setCreatedTime(time);
        serviceReview.setClerkId(param.getClerkId());
        serviceReview.setStoresId(storesId);
        serviceReview.setParentId(param.getUserId());
        serviceReview.setParentName(userName);
        serviceReview.setUserPhone(userPhone);
        serviceReview.setServeId(serveId);
        serviceReview.setComment(param.getComment());
        serviceReviewService.save(serviceReview);
    }

    @Override
    public Page<ServiceReview> getclerkReply(PageserviceReviewParam param) {
        //根据当前登录ID查询门店信息
        Result<Integer> result = userClient.findStoresIdByClerkId(param.getClerkId());
        Integer storesId = result.getData();
        //根据门店ID去查询所有当前门店的评论
        Page<ServiceReview> page = new Page<>(param.getPageNum() == null ? 1 : param.getPageNum(), param.getPageSize() == null ? 6 : param.getPageSize());
        QueryWrapper<ServiceReview> wrapper = new QueryWrapper<>();
        // 添加当前的门店ID作为查询条件
        wrapper.eq("stores_id", storesId).eq("parent_id",0);
        // 添加排序规则，比如按照创建时间降序排序
        wrapper.orderByDesc("created_time");
        Page<ServiceReview> serviceReviewPage = serviceReviewMapper.selectPage(page, wrapper);
        return serviceReviewPage;
    }

    @Override
    public Page<ServiceReview> getSellerResponse(PageserviceReviewParam param) {
        //根据当前登录ID查询门店信息
        Result<Integer> result = userClient.findStoresIdByClerkId(param.getClerkId());
        Integer storesId = result.getData();
        //根据门店ID去查询所有当前门店的评论
        Page<ServiceReview> page = new Page<>(param.getPageNum() == null ? 1 : param.getPageNum(), param.getPageSize() == null ? 6 : param.getPageSize());
        QueryWrapper<ServiceReview> wrapper = new QueryWrapper<>();
        // 添加当前的门店ID作为查询条件
        wrapper.eq("stores_id", storesId);
        // 添加查询clerkId不为空的条件
        wrapper.isNotNull("clerk_id");
        // 添加排序规则，比如按照创建时间降序排序
        wrapper.orderByDesc("created_time");
        Page<ServiceReview> serviceReviewPage = serviceReviewMapper.selectPage(page, wrapper);
        return serviceReviewPage;
    }

    @Override
    public Page<ServiceReview> getUserReply(getUserReplyForm form) {
        Page<ServiceReview> page = new Page<>(form.getPageNum() == null ? 1 : form.getPageNum(), form.getPageSize() == null ? 6 : form.getPageSize());
        //根据父级ID去查询所有回复
        QueryWrapper<ServiceReview> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_id",form.getUserId());
        Page<ServiceReview> serviceReviewPage = serviceReviewMapper.selectPage(page, wrapper);
        return serviceReviewPage;
    }
}
